package com.yicong.ants.utils;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;

import com.cchao.simplelib.core.JsonHelper;
import com.cchao.simplelib.core.Logs;
import com.cchao.simplelib.core.UiHelper;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.yicong.ants.Constants;
import com.yicong.ants.bean.ShareBean;
import com.yicong.ants.manager.ImageHelper;

import java.io.File;

import cn.magicwindow.ShareHelper;

/**
 * Description :
 *
 * @author ZhJiao
 * @date 2021/9/26
 */
public class ShareUtils {

    /**
     * 友盟分享
     * @param activity
     * @param media
     * @param image
     */
    public static void UMShareImage(Activity activity, SHARE_MEDIA media, UMImage image) {
        new ShareAction(activity)
                .setPlatform(media)//传入平台
                .withText("蚁丛")//分享内容
                .withMedia(image)
                .setCallback(listener)//回调监听器
                .share();
    }

    public static void shareImage(Context mContext, Bitmap bitmap, ShareBean bean) {
        Uri uriToImage = Uri.parse(MediaStore.Images.Media.insertImage(
                mContext.getContentResolver(), bitmap, null, null));
        ComponentName cn = new ComponentName(Constants.YChat_package_name, Constants.YChat_Activity);
        Intent intent = new Intent(Constants.YChat_Action);
        intent.setComponent(cn);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_TEXT, JsonHelper.toJson(bean));
        intent.putExtra(Intent.EXTRA_STREAM,uriToImage.toString());
        intent.putExtra("type", 3);
        mContext.startActivity(intent);
    }

    public static void shareContent(Context mContent,ShareBean bean) {
        try {
            ComponentName cn = new ComponentName(Constants.YChat_package_name, Constants.YChat_Activity);
            Intent intent = new Intent(Constants.YChat_Action);
            intent.setComponent(cn);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, JsonHelper.toJson(bean));
            intent.putExtra("type", 3);
            mContent.startActivity(intent);
        } catch (Exception e) {
            UiHelper.showToast("分享出错，请更新目标 App 版本后再试");
        }
    }

    private static final UMShareListener listener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {

        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            UiHelper.showToast(throwable.getMessage());
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {

        }
    };
}
